package com.yzd.xander.dao.impl;

import com.yzd.xander.dao.UserDao;
import com.yzd.xander.entity.Users;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by YE on 2018-06-06.
 */
@Repository("userDao")
@Transactional(readOnly = false)
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

    @Override
    public Users selectUser(Users user) {
        List<Users> list= (List<Users>) getHibernateTemplate().find("from Users where username=? and password=?", user.getUsername(),user.getPassword());
        return list.size()>0?list.get(0):null;
    }

    @Override
    public boolean isExist(String username) {
        return getHibernateTemplate().find("from Users where username=?", username).size() > 0;
    }

    @Override
    public int addUser(Users user) {
        try {
            getHibernateTemplate().save(user);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    @Autowired
    public void setMySessionFactory(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }
}
